package com.briup.MR.funMR;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
import org.apache.hadoop.mapreduce.lib.map.InverseMapper;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;



/**
 * yarn jar HdfsClientDemo-1.0-SNAPSHOT.jar com.briup.MR.funMR.InverseMR -D input=/user/hdfs/course/course.txt -D output=/user/hdfs/course/inv_result
 */
public class InverseMR extends Configured implements Tool {
    public int run(String[] strings) throws Exception {
        Configuration conf = getConf();
        String input = conf.get("input");
        String output = conf.get("output");
        //设置拆分数据的标准
        conf.set("mapreduce.input.keyvaluelinerecordreader.key.value.separator"," ");

        Job job = Job.getInstance(conf);
        job.setJarByClass(this.getClass());
        job.setJobName("InverseMR");
        /*
        InverseMapper输出的键值类型与输出的键值类型对调
        eg   输入 LongWritable Text
             输出 Text         LongWritable
             总结。InverseMapper输出键值类型和读的处理器有关
         */
        job.setMapperClass(InverseMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        job.setNumReduceTasks(0);

        job.setInputFormatClass(KeyValueTextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);
        KeyValueTextInputFormat.addInputPath(job,new Path(input));
        TextOutputFormat.setOutputPath(job,new Path(output));
        return job.waitForCompletion(true)?0:-1;
    }

    public static void main(String[] args) throws Exception {
        System.exit(new ToolRunner().run(new InverseMR(),args));
    }
}
